package uk.ac.aber.paws.client.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
import java.util.logging.Level;
import static uk.ac.aber.paws.client.constants.CommandConstants.*;
import uk.ac.aber.paws.client.core.*;

/**
 * @author Rob Johnson, Aberystwyth University
 * @email rob.johnson@aber.ac.uk
 * @date 16th August 2011
 * 
 * WMIOperations.java
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class WMIOperations {

	/**
	 * Get the serial number of this computer using WMI
	 * @return The serial number of the computer
	 */
	public synchronized static String getSerialNumber() {
		
		String line = "unavailable";
		
		try {
			Process p = Runtime.getRuntime().exec(COMMAND_SERIAL_NUMBER);
			
			p.waitFor();
			
			BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
			 
			while ((line = reader.readLine()) != null) {
				
				// check the required field begins with SerialNumber
				if (!line.startsWith("SerialNumber") && line.length() > 0) {
					line = line.toUpperCase();
					break;
				}
			}
			
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		
		return line;
	}
	
	/**
	 * Gets a vector list of users logged in to the computer
	 * @return A Vector storing each user account logged in - there are probably 4 blank entries and one genuine 
	 * user name returned. If a user is not logged in, then it is possible that the wrapper service user account is returned.
	 */
	public static Vector<String> getUserLoggedIn() {
		
		String line = "";
		Vector<String> tmpVector = new Vector<String>();
		
		try {
			Process p = Runtime.getRuntime().exec(COMMAND_LOGGED_IN_USER);
			p.waitFor();
			
			BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
		    
			while ((line = reader.readLine()) != null) {
				
				// check the required field begins with UserName
				if (!line.startsWith("UserName")) {
				
					line = line.toUpperCase();
					
					tmpVector.add(line);
				}
			}
		}
		catch (InterruptedException ex) {
			System.out.println("Thread stopped");
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		
		return tmpVector;
	}
	
	/**
	 * Gets a vector list of the network cards MAC addresses with associated IP address
	 */
	public static Vector<Hashtable<String,String>> getMacAddresses() {
		
		String line = "";
		Vector<Hashtable<String,String>> tmpVector = new Vector<Hashtable<String,String>>();
		
		try {
			Process p = Runtime.getRuntime().exec(COMMAND_MACIP_INFO);
			p.waitFor();
			
			BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
		    
			while ((line = reader.readLine()) != null) {
				
				// check the required field begins with UserName
				if (!line.startsWith("IPAddress")) {
					
					line = line.trim();
					
					if (line.length() == 67) {
						Hashtable<String,String> tmpHash = new Hashtable<String,String>();
					
						tmpHash.put("IP", line.substring(2, 17).trim());
						tmpHash.put("MAC", line.substring(50, 67).trim());
					
						tmpVector.add(tmpHash);
					}
				}
			}
		}
		catch (InterruptedException ex) {
			System.out.println("Thread stopped");
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		
		return tmpVector;
	}
	
	/**
	 * Gets a vector list of users logged in to the computer - NEW IMPLEMENTATION USING QWINSTA
	 * @return A Vector storing each user account logged in.
	 */
	public static Vector<String> getLoggedInUserList() {
		
		String line = "";
		Vector<String> tmpVector = new Vector<String>();
		
		try {
			Process p = Runtime.getRuntime().exec(COMMAND_LOGGED_IN_WINDOWS);
			
			p.waitFor();
			
			BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
		    
			while ((line = reader.readLine()) != null) {
				
				Initiator.LOG_FILE.log(Level.INFO,"--"+line);
				
				if (line.contains("console")) {
					StringTokenizer st = new StringTokenizer(line," ");
					
					while (st.hasMoreElements()) {
						Initiator.LOG_FILE.log(Level.INFO,(String)st.nextElement());
					}
				}
				// check the required field begins with UserName
				//if (!line.startsWith("UserName")) {
				
					//line = line.toUpperCase();
					
					//tmpVector.add(line);
				//}
			}
		}
		catch (InterruptedException ex) {
			System.out.println("Thread stopped");
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		
		return tmpVector;
	}
	
	/**
	 * Gets a vector list of users logged in to the computer - NEW IMPLEMENTATION USING QWINSTA
	 * @return A Vector storing each user account logged in.
	 */
	public static Vector<Hashtable<String,String>> getVolumeList() {
		
		String line = "";
		Vector<Hashtable<String,String>> tmpVector = new Vector<Hashtable<String,String>>();
		
		try {
			Process p = Runtime.getRuntime().exec(COMMAND_VOLUME_INFO);
			
			p.waitFor();
			
			BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
		    
			// discard 1st line
			line = reader.readLine();
			
			while ((line = reader.readLine()) != null) {
				
				if (line.length() > 14) {
					Hashtable<String,String> tmpHash = new Hashtable<String,String>();
				
					tmpHash.put("DriveLetter", line.substring(0, 13).trim());
					tmpHash.put("DriveType", line.substring(13, 14).trim());
				
					tmpVector.add(tmpHash);
				}
			}
		}
		catch (InterruptedException ex) {
			System.out.println("Thread stopped");
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		
		return tmpVector;
	}
}
